// ScriptureRef.cpp : implementation file
//

#include "stdafx.h"
#include "SwordBible.h"
#include "ScriptureRef.h"
#include "BibleSheet.h"
#include "CrossLinksMgr.h"

// CScriptureRef dialog

IMPLEMENT_DYNAMIC(CScriptureRef, CPropertyPage)

CScriptureRef::CScriptureRef()
	: CPropertyPage(CScriptureRef::IDD)
{

}

CScriptureRef::~CScriptureRef()
{
}

void CScriptureRef::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RICHEDIT_SCRIPREF, richCtrl);
}


BEGIN_MESSAGE_MAP(CScriptureRef, CPropertyPage)
  ON_COMMAND(ID_CROSSLINKSMGR, OnCrossLinksMgr)
  ON_COMMAND(ID_SCRIPREF_COPY, OnCopy)
  ON_COMMAND(ID_SCRIPREF_SELECTALL, OnSelectAll)
  ON_COMMAND(ID_SCRIPREF_ZOOM, OnZoom)
	ON_UPDATE_COMMAND_UI(ID_SCRIPREF_ZOOM, OnUpdateZoom)
	ON_WM_SIZE()
END_MESSAGE_MAP()


// CScriptureRef message handlers
void CScriptureRef::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);

	CWnd* pWnd = GetDlgItem(IDC_RICHEDIT_SCRIPREF);

	// resize the rich edit  control
	if( ::IsWindow(pWnd ->GetSafeHwnd() )) 
	pWnd ->SetWindowPos(NULL,10,40,cx-20,cy-50,SWP_NOZORDER | SWP_NOACTIVATE);



	// TODO: Add your message handler code here
}

void CScriptureRef::SetText(CString text)
{
	if(  ((CBibleSheet*)GetParent())->GetActivePage() != this) {
	((CBibleSheet*)GetParent())->SetActivePage(this);
	}

	//words of christ in red
	text.Replace(_T("\\cf6"),_T("\\cf9"));

	// footnote tags are enclosed between href tags in bibles. Footnotes use color cf5 in the color table 
	// x reference tags are enclosed between href tags in bibles. cross references use color cf6 in the color table 
	text.Replace(_T("<a href=\"\">*n"),  _T("\\cf5\\b *n"));
	text.Replace(_T("<a href=\"\">*x"),  _T("\\cf6\\b *x"));
	text.Replace(_T("</a>"), _T("\\cf2\\b0 "));
	text.Replace(_T("<a href=\"\">"),  _T(" "));

	richCtrl.SetRTF(text);

  ZoomText();
}

BOOL CScriptureRef::OnInitDialog()
{
	CDialog::OnInitDialog();

	richCtrl.SetEventMask(ENM_MOUSEEVENTS | ENM_KEYEVENTS | ENM_SCROLLEVENTS);
	richCtrl.SendMessage(EM_EXLIMITTEXT,0,-1);

  LoadToolbar();
	
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}


// this function loads the toolbar on the bible page
int CScriptureRef::LoadToolbar(void)
{
	
  if (!m_wndFormatBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD
       | WS_VISIBLE | CBRS_TOP |  CBRS_FLYBY | CBRS_TOOLTIPS) ||
       !m_wndFormatBar.LoadToolBar(IDR_TOOLBAR_SCRIPREF))
   {
      TRACE0("Failed to create toolbar\n");
      return -1;      // fail to create
   }

   m_wndFormatBar.LoadTCToolBar(24, IDB_TOOLBAR_SCRIPREF, IDB_TOOLBAR_SCRIPREF,
                               IDB_TOOLBAR_SCRIPREF, RGB(255,255,255));


	// Position the control bars using RepositionBars. 
	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);

	// Expand the size of the Toolbar window so all controls in it would fit
	CRect toolBarRect;
	CToolBarCtrl& formatBar = m_wndFormatBar.GetToolBarCtrl();
	formatBar.GetWindowRect(toolBarRect);
	ScreenToClient(toolBarRect);
	toolBarRect.left += 10  ;
	toolBarRect.right += 500  ;
	formatBar.MoveWindow(toolBarRect,TRUE);

  return 0;
}

void CScriptureRef::OnCrossLinksMgr()
{
   CCrossLinksMgr dlg;  
   dlg.DoModal();
}

void CScriptureRef::OnCopy() 
{
	// only the active page should handle the message
  if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;

  richCtrl.Copy();

 }

void CScriptureRef::OnSelectAll()
{
		// only the active page should handle the message
  if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;

  richCtrl.SetSel(0,-1);

}

void CScriptureRef::OnZoom()
{
	// only the active page should handle the message
		if(  !( ((CPropertySheet*) GetParent())->GetActivePage()   == this) )  
	  return;

	CSwordBibleDoc::scripRefZoom = !(CSwordBibleDoc::scripRefZoom);

	ZoomText();

}

void CScriptureRef::ZoomText()
{
	if(CSwordBibleDoc::scripRefZoom) richCtrl.SendMessage(EM_SETZOOM, 5,4); //zoom by 1.25
	else richCtrl.SendMessage(EM_SETZOOM, 0,0);  // turn off zooming
}


void CScriptureRef::OnUpdateZoom(CCmdUI *pCmdUI)
{
	pCmdUI->SetCheck(CSwordBibleDoc::scripRefZoom);	
}